#include <stdlib.h>
#include "yoc_bt_main.h"
#include "yoc_bt_device.h"
#include "yoc_gap_bt_api.h"
#include "yoc_a2dp_api.h"
#include "yoc_avrc_api.h"
#include "yoc_gap_ble_api.h"
#include "yoc_gatts_api.h"
#include <aos/aos.h>
#include <devices/rtl8723ds.h>
#include <board_config.h>
#include <ulog/ulog.h>
#include "app_bt.h"

#define TAG        "APP_BT"
#define KV_BT_NAME "bt_device_name"

static const char *s_a2d_conn_state_str[]  = {"Disconnected", "Connecting", "Connected",
                                             "Disconnecting"};
static const char *s_a2d_audio_state_str[] = {"Suspended", "Stopped", "Started"};

static int g_a2dp_player_status = -1;

static app_bt_callback_t g_bt_cb = NULL;

static uint8_t _bt_get_lable(void)
{
    static uint8_t lable = 0;

    lable++;
    lable = lable % 15;

    return lable;
}

static void _bt_gap_cb(yoc_bt_gap_cb_event_t event, yoc_bt_gap_cb_param_t *param)
{
    switch (event) {
    case YOC_BT_GAP_AUTH_CMPL_EVT: {
        if (param->auth_cmpl.stat == YOC_BT_STATUS_SUCCESS) {
            LOGD(TAG, "authentication success: %s", param->auth_cmpl.device_name);

            if (g_bt_cb) {
                app_bt_param_t param_m;
                memcpy(param_m.paired.remote_addr, param->auth_cmpl.bda, 6);
                g_bt_cb(APP_BT_PAIRED, &param_m);
            }
        } else {
            LOGE(TAG, "authentication failed, status:%d", param->auth_cmpl.stat);

            // _tg_bt_paired_result_cb(param->auth_cmpl.stat, param->auth_cmpl.bda);
        }
        break;
    }
    case YOC_BT_GAP_CFM_REQ_EVT:
        LOGD(TAG, "YOC_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d",
             param->cfm_req.num_val);
        // YOC_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
        break;
    case YOC_BT_GAP_KEY_NOTIF_EVT:
        LOGD(TAG, "YOC_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
        break;
    case YOC_BT_GAP_KEY_REQ_EVT:
        LOGD(TAG, "YOC_BT_GAP_KEY_REQ_EVT Please enter passkey!");
        break;
    case YOC_BT_GAP_READ_RSSI_DELTA_EVT:
        LOGD(TAG, "rssi delta %d", param->read_rssi_delta.rssi_delta);
        break;
    case YOC_BT_GAP_CONFIG_EIR_DATA_EVT:
        LOGD(TAG, "EIR DATA %d", param->config_eir_data.stat);
        break;
    default: {
        LOGE(TAG, "unhandled event: %d", event);
        break;
    }
    }
    return;
}

static void _bt_app_a2d_cb(yoc_a2d_cb_event_t event, yoc_a2d_cb_param_t *param)
{
    yoc_a2d_cb_param_t *a2d = (yoc_a2d_cb_param_t *)(param);

    LOGD(TAG, "%s evt %d", __func__, event);
    switch (event) {
    case YOC_A2D_CONNECTION_STATE_EVT: {
        uint8_t *bda;

        bda = a2d->conn_stat.remote_bda;
        LOGD(TAG, "A2DP connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]",
             s_a2d_conn_state_str[a2d->conn_stat.state], bda[0], bda[1], bda[2], bda[3], bda[4],
             bda[5]);
        if (a2d->conn_stat.state == YOC_A2D_CONNECTION_STATE_DISCONNECTED) {
            if (a2d->conn_stat.disc_rsn == YOC_A2D_DISC_RSN_NORMAL) {
                if (g_bt_cb) {
                    app_bt_param_t param;
                    memcpy(param.a2dp_conn.remote_addr, bda, 6);
                    g_bt_cb(APP_BT_A2DP_DISCONNECTED, &param);
                }
            } else {
                if (g_bt_cb) {
                    app_bt_param_t param;
                    memcpy(param.a2dp_conn.remote_addr, bda, 6);
                    g_bt_cb(APP_BT_A2DP_LINK_LOSS, &param);
                }
            }
        } else if (a2d->conn_stat.state == YOC_A2D_CONNECTION_STATE_CONNECTED) {
            if (g_bt_cb) {
                app_bt_param_t param;
                memcpy(param.a2dp_conn.remote_addr, bda, 6);
                g_bt_cb(APP_BT_A2DP_CONNECTED, &param);
            }
        } else if (a2d->conn_stat.state == YOC_A2D_CONNECTION_STATE_CONNECTING) {
            if (g_bt_cb) {
                app_bt_param_t param;
                memcpy(param.a2dp_conn.remote_addr, bda, 6);
                g_bt_cb(APP_BT_A2DP_CONNECTING, &param);
            }
        }
        break;
    }
    case YOC_A2D_AUDIO_STATE_EVT: {
        LOGD(TAG, "A2DP audio state: %s", s_a2d_audio_state_str[a2d->audio_stat.state]);
        // g_tg_bt_audio_state = a2d->audio_stat.state;
        if (YOC_A2D_AUDIO_STATE_STARTED == a2d->audio_stat.state) {
            if (g_bt_cb) {
                g_bt_cb(APP_BT_A2DP_PLAY_STATUS_PLAYING, NULL);
            }
            g_a2dp_player_status = 1;
        } else if (YOC_A2D_AUDIO_STATE_REMOTE_SUSPEND == a2d->audio_stat.state ||
                   YOC_A2D_AUDIO_STATE_STOPPED == a2d->audio_stat.state) {
            if (g_bt_cb) {
                g_bt_cb(APP_BT_A2DP_PLAY_STATUS_STOPPED, NULL);
            }
            g_a2dp_player_status = 0;
        }

        break;
    }
    case YOC_A2D_AUDIO_CFG_EVT: {
        LOGD(TAG, "A2DP audio stream configuration, codec type %d", a2d->audio_cfg.mcc.type);
        // for now only SBC stream is supported
        if (a2d->audio_cfg.mcc.type == YOC_A2D_MCT_SBC) {
            LOGD(TAG, "Configure audio player %x-%x-%x-%x", a2d->audio_cfg.mcc.cie.sbc[0],
                 a2d->audio_cfg.mcc.cie.sbc[1], a2d->audio_cfg.mcc.cie.sbc[2],
                 a2d->audio_cfg.mcc.cie.sbc[3]);
        }
        break;
    }
    default:
        LOGE(TAG, "%s unhandled evt %d", __func__, event);
        break;
    }
}

static void bt_av_new_track()
{
    //Register notifications and request metadata
    yoc_avrc_ct_send_metadata_cmd(_bt_get_lable(),
                                  YOC_AVRC_MD_ATTR_TITLE | YOC_AVRC_MD_ATTR_ARTIST |
                                      YOC_AVRC_MD_ATTR_ALBUM | YOC_AVRC_MD_ATTR_GENRE);
    yoc_avrc_ct_send_register_notification_cmd(_bt_get_lable(), YOC_AVRC_RN_TRACK_CHANGE, 0);
}

static void _bt_avrcp_notify_evt_handler(uint8_t event_id, yoc_avrc_rn_param_t *event_parameter)
{
    LOGE(TAG, "%s %d", __func__, event_id);

    switch (event_id) {
    case YOC_AVRC_RN_TRACK_CHANGE:
        bt_av_new_track();
        break;
    case YOC_AVRC_RN_VOLUME_CHANGE:
        /* FIXME */
        if (g_bt_cb) {
            app_bt_param_t param;
            param.a2dp_vol.volume = event_parameter->volume;
            g_bt_cb(APP_BT_A2DP_VOLUME_CHANGE, &param);
        }
        break;
    case YOC_AVRC_RN_PLAY_STATUS_CHANGE: {
        app_bt_event_t status;

        switch (event_parameter->playback) {
        case YOC_AVRC_PLAYBACK_STOPPED:
            status = APP_BT_AVRCP_STATUS_STOPPED;
            break;
        case YOC_AVRC_PLAYBACK_PLAYING:
            status = APP_BT_AVRCP_STATUS_PLAYING;
            break;
        case YOC_AVRC_PLAYBACK_PAUSED:
            status = APP_BT_AVRCP_STATUS_PAUSEED;
            break;
        default:
            return;
        }
        yoc_avrc_ct_send_register_notification_cmd(_bt_get_lable(), YOC_AVRC_RN_PLAY_STATUS_CHANGE,
                                                   0);

        if (g_bt_cb) {
            g_bt_cb(status, NULL);
        }

        break;
    }
    }
}

static void _bt_app_avrcp_ct_cb(yoc_avrc_ct_cb_event_t event, yoc_avrc_ct_cb_param_t *param)
{
    yoc_avrc_ct_cb_param_t *rc = (yoc_avrc_ct_cb_param_t *)(param);

    LOGD(TAG, "%s evt %d", __func__, event);
    switch (event) {
    case YOC_AVRC_CT_CONNECTION_STATE_EVT: {
        uint8_t *bda = rc->conn_stat.remote_bda;
        LOGD(TAG, "AVRC conn_state evt: state %d, [%02x:%02x:%02x:%02x:%02x:%02x]",
             rc->conn_stat.connected, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);

        if (rc->conn_stat.connected) {
            bt_av_new_track();
            yoc_avrc_ct_send_register_notification_cmd(_bt_get_lable(),
                                                       YOC_AVRC_RN_PLAY_STATUS_CHANGE, 0);
        }
        break;
    }
    case YOC_AVRC_CT_PASSTHROUGH_RSP_EVT: {
        app_avrcp_cmd_type_t cmd;

        LOGD(TAG, "AVRC passthrough rsp: key_code 0x%x, key_state %d", rc->psth_rsp.key_code,
             rc->psth_rsp.key_state);
        switch (rc->psth_rsp.key_code) {
        case YOC_AVRC_PT_CMD_PLAY:
            cmd = APP_BT_AVRCP_CMD_PLAY;
            break;
        case YOC_AVRC_PT_CMD_PAUSE:
            cmd = APP_BT_AVRCP_CMD_PAUSE;
            break;
        case YOC_AVRC_PT_CMD_FORWARD:
            cmd = APP_BT_AVRCP_CMD_FORWARD;
            break;
        case YOC_AVRC_PT_CMD_BACKWARD:
            cmd = APP_BT_AVRCP_CMD_BACKWARD;
            break;
        case YOC_AVRC_PT_CMD_FAST_FORWARD:
            cmd = APP_BT_AVRCP_CMD_FAST_FORWARD;
            break;
        case YOC_AVRC_PT_CMD_REWIND:
            cmd = APP_BT_AVRCP_CMD_REWIND;
            break;
        case YOC_AVRC_PT_CMD_STOP:
            cmd = APP_BT_AVRCP_CMD_STOP;
            break;
        default:
            return;
        }

        if (g_bt_cb) {
            app_bt_param_t param;
            param.a2dp_cmd.cmd = cmd;
            g_bt_cb(APP_BT_A2DP_CMD, &param);
        }
        break;
    }
    case YOC_AVRC_CT_METADATA_RSP_EVT: {
        uint8_t *attr_text;

        attr_text = (uint8_t *)malloc(rc->meta_rsp.attr_length + 1);
        if (attr_text == NULL) {
            LOGD(TAG, "attr_text malloc failed");
            return;
        }
        memcpy(attr_text, rc->meta_rsp.attr_text, rc->meta_rsp.attr_length);
        attr_text[rc->meta_rsp.attr_length] = 0;

        LOGD(TAG, "AVRC metadata rsp: attribute id 0x%x, %s", rc->meta_rsp.attr_id, attr_text);
        free(attr_text);
        break;
    }
    case YOC_AVRC_CT_CHANGE_NOTIFY_EVT: {
        LOGD(TAG, "AVRC event notification: %d, param: %d", rc->change_ntf.event_id,
             rc->change_ntf.event_parameter);
        _bt_avrcp_notify_evt_handler(rc->change_ntf.event_id, &rc->change_ntf.event_parameter);
        break;
    }
    case YOC_AVRC_CT_REMOTE_FEATURES_EVT: {
        LOGD(TAG, "AVRC remote features %x", rc->rmt_feats.feat_mask);
        break;
    }
    case YOC_AVRC_CT_PLAY_STATUS_RSP_EVT: {
        LOGD(TAG, "AVRC PLAYER features %x", rc->rmt_feats.feat_mask);
    }
    case YOC_AVRC_CT_SET_ABSOLUTE_VOLUME_RSP_EVT: {
        LOGD(TAG, "AVRC VOLUME set %d", rc->set_volume_rsp.volume);
        if (g_bt_cb) {
            app_bt_param_t param;
            param.a2dp_vol.volume = rc->set_volume_rsp.volume;
            g_bt_cb(APP_BT_A2DP_VOLUME_CHANGE, &param);
        }
    }
    default:
        LOGE(TAG, "%s unhandled evt %d", __func__, event);
        break;
    }
}

static void _bt_app_avrcp_tg_cb(yoc_avrc_tg_cb_event_t event, yoc_avrc_tg_cb_param_t *param)
{
    yoc_avrc_tg_cb_param_t *rc = (yoc_avrc_tg_cb_param_t *)(param);

    LOGD(TAG, "%s evt %d", __func__, event);
    switch (event) {
    case YOC_AVRC_TG_REGISTER_NOTIFICATION_EVT: {
        if (rc->reg_ntf.event_id == YOC_AVRC_RN_VOLUME_CHANGE) {
            yoc_avrc_rn_param_t param;

            param.volume = 0x7f;
            yoc_avrc_tg_send_rn_rsp(YOC_AVRC_RN_VOLUME_CHANGE, YOC_AVRC_RN_RSP_INTERIM, &param);
        }
    } break;
    case YOC_AVRC_TG_SET_ABSOLUTE_VOLUME_CMD_EVT: {
        LOGD(TAG, "volume: %d", rc->set_abs_vol.volume);
        if (g_bt_cb) {
            app_bt_param_t param;
            param.a2dp_vol.volume = rc->set_abs_vol.volume;
            g_bt_cb(APP_BT_A2DP_VOLUME_CHANGE, &param);
        }
    } break;
    default:;
    }
}

static void _tg_bt_app_a2d_data_cb(const uint8_t *data, uint32_t len)
{
    static uint32_t s_pkt_cnt = 0;

    if (++s_pkt_cnt % 100 == 0) {
        LOGD(TAG, "Audio packet count %u", s_pkt_cnt);
    }
}

int app_bt_init()
{
    bt_err_t   err;
    static int bluedroid_init = 0;
    if (!bluedroid_init) {

        rtl8723ds_bt_config config = {
            .uart_id    = BT_UART_IDX,
            .bt_dis_pin = BT_DIS_PIN,
        };

        bt_rtl8723ds_register(&config);

        err = yoc_bluedroid_init();
        if (err != BT_OK) {
            LOGE(TAG, "yoc_bluedroid_init err= %d", err);
            return -1;
        }

        err = yoc_bluedroid_enable();
        if (err != BT_OK) {
            LOGE(TAG, "yoc_bluedroid_enable err= %d", err);
            return -1;
        }

        err = yoc_bt_gap_register_callback(_bt_gap_cb);
        if (err) {
            LOGE(TAG, "gap register error, error code = %x", err);
            return -1;
        }
        bluedroid_init = 1;
    }

    return 0;
}
#define DEVICE_NAME_FORMAT "5654-%4d"
int app_bt_a2dp_init()
{
    char dev_name[YOC_DEV_DEVICE_NAME_MAX] = {0};

    if (g_a2dp_player_status >= 0) {
        return 0;
    }
    g_a2dp_player_status = 0;

    yoc_a2d_register_callback(_bt_app_a2d_cb);
    yoc_a2d_sink_register_data_callback(_tg_bt_app_a2d_data_cb);
    yoc_a2d_sink_init();

    /* initialize AVRCP controller */
    yoc_avrc_ct_init();
    yoc_avrc_ct_register_callback(_bt_app_avrcp_ct_cb);

    yoc_avrc_tg_init();
    yoc_avrc_tg_register_callback(_bt_app_avrcp_tg_cb);

    // yoc_avrc_rn_evt_cap_mask_t evt_set;

    /* volume sync setup for IOS volume control */
    // evt_set.bits = 0x2000;
    // yoc_avrc_tg_set_rn_evt_cap(&evt_set);

    yoc_bt_io_cap_t bt_io_cap  = YOC_BT_IO_CAP_NONE;
    yoc_bt_io_cap_t ble_io_cap = YOC_BT_IO_CAP_NONE;

    yoc_bt_sp_param_t param_type_bt = YOC_BT_SP_IOCAP_MODE;
    yoc_bt_gap_set_security_param(param_type_bt, &bt_io_cap, sizeof(yoc_bt_io_cap_t));

    yoc_ble_sm_param_t param_type_ble = YOC_BLE_SM_IOCAP_MODE;
    yoc_ble_gap_set_security_param(param_type_ble, &ble_io_cap, sizeof(yoc_bt_io_cap_t));
    // 初始化默认不广播
    // yoc_bt_gap_set_scan_mode(YOC_BT_CONNECTABLE, YOC_BT_GENERAL_DISCOVERABLE);

    //app_sys_get_device_name(device_name);
    if (aos_kv_getstring(KV_BT_NAME, dev_name, sizeof(dev_name)) <= 0) {
        int bt_name_tail = rand() % 10000;
        sprintf(dev_name, DEVICE_NAME_FORMAT, bt_name_tail);
    }
    LOGD(TAG, "BT name: %s", dev_name);
    app_bt_gap_set_device_name(dev_name);
    return 0;
}

int app_bt_gap_set_scan_mode(int enable)
{
    if (enable == 0) {
        yoc_bt_gap_set_scan_mode(YOC_BT_NON_CONNECTABLE, YOC_BT_NON_DISCOVERABLE);
    } else {
        yoc_bt_gap_set_scan_mode(YOC_BT_CONNECTABLE, YOC_BT_GENERAL_DISCOVERABLE);
    }

    return 0;
}

int app_bt_gap_set_device_name(char *name)
{
    int  ret                               = 0;
    char dev_name[YOC_DEV_DEVICE_NAME_MAX] = {0};
    ret = aos_kv_getstring(KV_BT_NAME, dev_name, sizeof(dev_name));
    if ((ret <=0) || (strcmp(name, dev_name) != 0)) {
        ret = aos_kv_setstring(KV_BT_NAME, name);
        if (ret != 0) {
            LOGE(TAG, "set bt_device_name failed!");
            return -1;
        }
    }

    return yoc_bt_dev_set_device_name(name);
}

int app_bt_a2dp_connect(uint8_t remote_addr[])
{
    return yoc_a2d_sink_connect(remote_addr);
}

int app_bt_a2dp_disconnect(uint8_t remote_addr[])
{
    return yoc_a2d_sink_disconnect(remote_addr);
}

int app_bt_avrcp_send_passthrouth_cmd(app_avrcp_cmd_type_t cmd_type)
{
    int32_t cmd;

    LOGD(TAG, "%s start: %d", __func__, cmd_type);
    switch (cmd_type) {
    case APP_BT_AVRCP_CMD_PLAY:
        cmd = YOC_AVRC_PT_CMD_PLAY;
        break;
    case APP_BT_AVRCP_CMD_PAUSE:
        cmd = YOC_AVRC_PT_CMD_PAUSE;
        break;
    case APP_BT_AVRCP_CMD_FORWARD:
        cmd = YOC_AVRC_PT_CMD_FORWARD;
        break;
    case APP_BT_AVRCP_CMD_BACKWARD:
        cmd = YOC_AVRC_PT_CMD_BACKWARD;
        break;
    case APP_BT_AVRCP_CMD_FAST_FORWARD:
        cmd = YOC_AVRC_PT_CMD_FAST_FORWARD;
        break;
    case APP_BT_AVRCP_CMD_REWIND:
        cmd = YOC_AVRC_PT_CMD_REWIND;
        break;
    case APP_BT_AVRCP_CMD_STOP:
        cmd = YOC_AVRC_PT_CMD_STOP;
        break;
    default:
        return -1;
    }
    yoc_avrc_ct_send_passthrough_cmd(_bt_get_lable(), cmd, 0);
    yoc_avrc_ct_send_passthrough_cmd(_bt_get_lable(), cmd, 1);

    return 0;
}

int app_bt_avrcp_change_vol(uint8_t vol)
{
    yoc_avrc_rn_param_t param;

    LOGD(TAG, "%s start: %d", __func__, vol);
    param.volume = vol;
    return yoc_avrc_tg_send_rn_rsp(YOC_AVRC_RN_VOLUME_CHANGE, YOC_AVRC_RN_RSP_CHANGED, &param);
}

int app_bt_register_cb(app_bt_callback_t callback)
{
    g_bt_cb = callback;

    return 0;
}

int app_bt_a2dp_get_status()
{
    return (g_a2dp_player_status > 0);
}

#define BT_BD_ADDR_LEN 6
static unsigned char cur_bt_addr[BT_BD_ADDR_LEN] = {0};
static int           bt_connect_status       = 0;
int Bluetooth_ReConnect()
{
    int i   = 0;
    int t   = 0;
    int ret = 0;
    for (i = 0; i < BT_BD_ADDR_LEN; i++) {
        t += cur_bt_addr[i];
    }
    if (t == 0) {
        LOGD("Bluetooth_ReConnect", "Invalid bt_addr, abort reconnect.");
        return -1;
    }
    ret = app_bt_a2dp_connect(cur_bt_addr);
    LOGD(TAG, "Bluetooth_ReConnect remote_addr: [%02x:%02x:%02x:%02x:%02x:%02x], ret = %d.",
         cur_bt_addr[0], cur_bt_addr[1], cur_bt_addr[2], cur_bt_addr[3], cur_bt_addr[4],
         cur_bt_addr[5], ret);
    return ret;
}
static void bt_callback(app_bt_event_t event, app_bt_param_t *param)
{
    uint8_t *addr;

    switch (event) {
    case APP_BT_PAIRED:
        addr = param->paired.remote_addr;
        LOGD(TAG, "PAIRED remote_addr: %02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2],
             addr[3], addr[4], addr[5]);
        break;
    case APP_BT_A2DP_CONNECTED:
        addr = param->a2dp_conn.remote_addr;
        memcpy((void *)cur_bt_addr, (void *)param->a2dp_conn.remote_addr,
               BT_BD_ADDR_LEN); // 设置连接的MAC地址
        LOGD(TAG, "CONNECTED remote_addr: %02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2],
             addr[3], addr[4], addr[5]);
        app_bt_gap_set_scan_mode(0);
        bt_connect_status = 1;
        break;

    case APP_BT_A2DP_DISCONNECTED:
        addr = param->a2dp_conn.remote_addr;
        memset((void *)cur_bt_addr, 0, BT_BD_ADDR_LEN); // 清除连接的MAC地址
        LOGD(TAG, "DISCONNECTED remote_addr: %02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1],
             addr[2], addr[3], addr[4], addr[5]);
        app_bt_gap_set_scan_mode(1); //恢复蓝牙广播
        bt_connect_status = 0;
        break;

    case APP_BT_A2DP_CONNECTING:
        addr = param->a2dp_conn.remote_addr;
        LOGD(TAG, "CONNECTING remote_addr: %02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1],
             addr[2], addr[3], addr[4], addr[5]);
        break;

    case APP_BT_A2DP_LINK_LOSS:
        addr = param->a2dp_conn.remote_addr;
        LOGD(TAG, "LINK_LOSS remote_addr: %02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2],
             addr[3], addr[4], addr[5]);
        Bluetooth_ReConnect();
        break;

    case APP_BT_A2DP_PLAY_STATUS_STOPPED:
        LOGD(TAG, "APP_BT_A2DP_PLAY_STATUS_STOPPED");
        break;

    case APP_BT_A2DP_PLAY_STATUS_PLAYING:
        LOGD(TAG, "APP_BT_A2DP_PLAY_STATUS_PLAYING");
        break;

    case APP_BT_A2DP_PLAY_STATUS_PAUSEED:
        LOGD(TAG, "APP_BT_A2DP_PLAY_STATUS_PAUSEED");
        break;

    case APP_BT_AVRCP_STATUS_PAUSEED:
        LOGD(TAG, "APP_BT_AVRCP_STATUS_PAUSEED");
        break;
    case APP_BT_AVRCP_STATUS_STOPPED:
        LOGD(TAG, "APP_BT_AVRCP_STATUS_STOPPED");
        break;
    case APP_BT_AVRCP_STATUS_PLAYING:
        LOGD(TAG, "APP_BT_AVRCP_STATUS_PLAYING");
        break;

    case APP_BT_A2DP_VOLUME_CHANGE:
        LOGD(TAG, "VOLUME_CHANGE: %d", param->a2dp_vol.volume);
        break;

    case APP_BT_A2DP_CMD:
        break;
    default:
        break;
    }
}
/* 用户接口 */
int bt_init()
{
    static int bt_init_flag = 0;
    if(!bt_init_flag){
        LOGD(TAG, "BT init");
        app_bt_a2dp_init();
        app_bt_register_cb(bt_callback);
        bt_init_flag = 1;
    }
    return 0;
}
int bt_open()
{
    LOGD(TAG, "BT open");
    app_bt_gap_set_scan_mode(1);
    return 0;
}
int bt_close()
{
    LOGD(TAG, "BT close");
    if(bt_connect_status){
        extern bool bt_gatt_disconnectall(void);
        bt_gatt_disconnectall();
        app_bt_a2dp_disconnect(cur_bt_addr);
        memset((void *)cur_bt_addr, 0, BT_BD_ADDR_LEN); // 清除连接的MAC地址
    }
    app_bt_gap_set_scan_mode(0);
    return 0;
}
